<?php

namespace app\common\controller;

use think\Controller;
use think\Request;

class WxPay extends Controller
{
    public function toPay($orderInfo,$config){
        require_once VENDOR_PATH . 'WeixinPay/lib/WxPay.Config.php';
        if(!empty($config)){
            \WxPayConfig::$APPID = $config['appid'];
            \WxPayConfig::$MCHID = $config['mchid'];
            \WxPayConfig::$KEY = $config['key'];
            \WxPayConfig::$APPSECRET = $config['appsecret'];
        }
        require_once VENDOR_PATH . 'WeixinPay/lib/WxPay.Api.php';
        $input = new \WxPayUnifiedOrder();
        $input->SetBody($orderInfo['body']);
        //设置商品或支付单简要描述
        $input->SetAttach($orderInfo['product_id']);
        $input->SetOut_trade_no($orderInfo['pay_order_number']);
        //设置商户系统内部的订单号,32个字符内、可包含字母, 其他说明见商户订单号
        $input->SetTotal_fee($orderInfo['pay_amount'] * 100);
        //设置订单总金额，只能为整数，详见支付金额
        $input->SetTime_start(date("YmdHis"));
        //设置订单生成时间，格式为yyyyMMddHHmmss
        $input->SetTime_expire(date("YmdHis", time() + 60*10));
        //设置订单失效时间，格式为yyyyMMddHHmmss
        $input->SetNotify_url($orderInfo['notify_url']);
        //设置接收微信支付异步通知回调地址
        $input->SetTrade_type("MWEB");
        //设置类型如下：JSAPI，NATIVE，APP
        $order_data = \WxPayApi::unifiedOrder($input,6);
        if($order_data['return_code'] != 'SUCCESS'){
            dump($order_data['return_msg']);die;
        }
        if($order_data['result_code'] != 'SUCCESS'){
            dump($order_data['err_code_des']);die;
        }
        return $order_data['mweb_url']."&redirect_url=".urlencode($orderInfo['redirect_url']);
    }
    function JsApiPay($orderInfo,$openid,$config)
    {
        require_once VENDOR_PATH . "WeixinPay/lib/WxPay.Api.php";
        require_once VENDOR_PATH . "WeixinPay/lib/WxPay.JsApiPay.php";
        require_once VENDOR_PATH . "WeixinPay/lib/WxPay.Config.php";
        if(!empty($config)){
            \WxPayConfig::$APPID = $config['appid'];
            \WxPayConfig::$MCHID = $config['mchid'];
            \WxPayConfig::$KEY = $config['key'];
            \WxPayConfig::$APPSECRET = $config['appsecret'];
        }
        $tools = new \JsApiPay();
        $input = new \WxPayUnifiedOrder();
        $body = $orderInfo['body'] ? $orderInfo['body'] : $orderInfo['package_id']."-游戏充值";
        $input->SetBody($body);
        $input->SetAttach($orderInfo['product_id']);
        $input->SetOut_trade_no($orderInfo['pay_order_number']);
        $input->SetTotal_fee($orderInfo['pay_amount'] * 100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag("游戏道具");
        $input->SetNotify_url($orderInfo['notify_url']);
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openid);
        $order = \WxPayApi::unifiedOrder($input, 6);
        if($order['result_code'] == 'FAIL'){
            echo $order['err_code_des'];exit();
        }
        $jsApiParameters = $tools->GetJsApiParameters($order);
        return $jsApiParameters;
    }

    function SerJsApiPay($orderInfo,$openid,$config)
    {
        require_once VENDOR_PATH . "WeixinPay/lib/WxPay.Api.php";
        require_once VENDOR_PATH . "WeixinPay/lib/WxPay.JsApiPay.php";
        require_once VENDOR_PATH . "WeixinPay/lib/WxPay.Config.php";
        if(!empty($config)){
            \WxPayConfig::$APPID = $config['appid'];
            \WxPayConfig::$MCHID = $config['mchid'];
            \WxPayConfig::$KEY = $config['key'];
            \WxPayConfig::$APPSECRET = $config['appsecret'];
        }
        $tools = new \JsApiPay();
        $input = new \WxPayUnifiedOrder();
        $input->SetBody($orderInfo['ser_body']);
        $input->SetAttach("");
        $input->SetOut_trade_no($orderInfo['order_num']);
        $input->SetTotal_fee($orderInfo['price'] * 100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 600));
        $input->SetGoods_tag("");
        $input->SetNotify_url($orderInfo['notify_url']);
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openid);
        $order = \WxPayApi::unifiedOrder($input, 6);
        if($order['return_code'] == 'FAIL'){
            echo $order['return_msg'];exit();
        }
        $jsApiParameters = $tools->GetJsApiParameters($order);
        return $jsApiParameters;
    }

    function SerCodePay($orderInfo,$config)
    {
        require_once VENDOR_PATH . "WeixinPay/lib/WxPay.NativePay.php";
        require_once VENDOR_PATH . "WeixinPay/lib/WxPay.Config.php";
        if(!empty($config)){
            \WxPayConfig::$APPID = $config['appid'];
            \WxPayConfig::$MCHID = $config['mchid'];
            \WxPayConfig::$KEY = $config['key'];
            \WxPayConfig::$APPSECRET = $config['appsecret'];
        }
        $notify = new \NativePay();
        $input  = new \WxPayUnifiedOrder();
        $input->SetBody($orderInfo['ser_body']);
        $input->SetAttach("");
        $input->SetOut_trade_no($orderInfo['order_num']);
        $input->SetTotal_fee($orderInfo['price'] * 100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetGoods_tag("");
        $input->SetNotify_url($orderInfo['notify_url']);
        $input->SetTrade_type("NATIVE");
        $input->SetProduct_id('');
        try {
            $result = $notify->GetPayUrl($input);
            return $result["code_url"];
        } catch (\Exception $e) {
            var_dump($e->getMessage());
        }
    }


    public function toYunPay($orderInfo,$config){
        require_once VENDOR_PATH . 'WeixinPay/lib/WxPay.Config.php';
        if(!empty($config)){
            \WxPayConfig::$APPID = $config['appid'];
            \WxPayConfig::$MCHID = $config['mchid'];
            \WxPayConfig::$KEY = $config['key'];
            \WxPayConfig::$APPSECRET = $config['appsecret'];
        }
        require_once VENDOR_PATH . 'WeixinPay/lib/WxPay.Api.php';
        $input = new \WxPayUnifiedOrder();
        $input->SetBody($orderInfo['goods_name']);
        $input->SetAttach("");
        $input->SetOut_trade_no($orderInfo['order_num']);
        $input->SetTotal_fee($orderInfo['price'] * 100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 60*10));
        $input->SetNotify_url($orderInfo['notify_url']);
        $input->SetTrade_type("MWEB");
        $order_data = \WxPayApi::unifiedOrder($input,6);
        if($order_data['return_code'] != 'SUCCESS'){
            dump($order_data['return_msg']);die;
        }
        if($order_data['result_code'] != 'SUCCESS'){
            dump($order_data['err_code_des']);die;
        }
        return $order_data['mweb_url'];
    }
    function toYunCodePay($orderInfo,$config)
    {
        require_once VENDOR_PATH . "WeixinPay/lib/WxPay.NativePay.php";
        require_once VENDOR_PATH . "WeixinPay/lib/WxPay.Config.php";
        if(!empty($config)){
            \WxPayConfig::$APPID = $config['appid'];
            \WxPayConfig::$MCHID = $config['mchid'];
            \WxPayConfig::$KEY = $config['key'];
            \WxPayConfig::$APPSECRET = $config['appsecret'];
        }
        $notify = new \NativePay();
        $input  = new \WxPayUnifiedOrder();
        $input->SetBody($orderInfo['goods_name']);
        $input->SetAttach("");
        $input->SetOut_trade_no($orderInfo['order_num']);
        $input->SetTotal_fee($orderInfo['price'] * 100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 60*10));
        $input->SetGoods_tag("");
        $input->SetNotify_url($orderInfo['notify_url']);
        $input->SetTrade_type("NATIVE");
        $input->SetProduct_id('');
        try {
            $result = $notify->GetPayUrl($input);
            return $result["code_url"];
        } catch (\Exception $e) {
            var_dump($e->getMessage());
        }
    }

    function yunJsApiPay($orderInfo,$openid,$config)
    {
        require_once VENDOR_PATH . "WeixinPay/lib/WxPay.Api.php";
        require_once VENDOR_PATH . "WeixinPay/lib/WxPay.JsApiPay.php";
        require_once VENDOR_PATH . "WeixinPay/lib/WxPay.Config.php";
        if(!empty($config)){
            \WxPayConfig::$APPID = $config['appid'];
            \WxPayConfig::$MCHID = $config['mchid'];
            \WxPayConfig::$KEY = $config['key'];
            \WxPayConfig::$APPSECRET = $config['appsecret'];
        }
        $tools = new \JsApiPay();
        $input = new \WxPayUnifiedOrder();
        $input->SetBody($orderInfo['title']);
        $input->SetAttach('');
        $input->SetOut_trade_no($orderInfo['order_num']);
        $input->SetTotal_fee($orderInfo['discount_price'] * 100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 60*10));
        $input->SetGoods_tag("");
        $input->SetNotify_url($orderInfo['notify_url']);
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openid);
        $order = \WxPayApi::unifiedOrder($input, 6);
        if($order['result_code'] == 'FAIL'){
            echo $order['err_code_des'];exit();
        }
        $jsApiParameters = $tools->GetJsApiParameters($order);
        return $jsApiParameters;
    }


    public function toRechargePay($orderInfo,$config){
        require_once VENDOR_PATH . 'WeixinPay/lib/WxPay.Config.php';
        if(!empty($config)){
            \WxPayConfig::$APPID = $config['appid'];
            \WxPayConfig::$MCHID = $config['mchid'];
            \WxPayConfig::$KEY = $config['key'];
            \WxPayConfig::$APPSECRET = $config['appsecret'];
        }
        require_once VENDOR_PATH . 'WeixinPay/lib/WxPay.Api.php';
        $input = new \WxPayUnifiedOrder();
        $input->SetBody($orderInfo['title']);
        $input->SetAttach("");
        $input->SetOut_trade_no($orderInfo['order_num']);
        $input->SetTotal_fee($orderInfo['discount_price'] * 100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 60*10));
        $input->SetNotify_url($orderInfo['notify_url']);
        $input->SetTrade_type("MWEB");
        $order_data = \WxPayApi::unifiedOrder($input,6);
        if($order_data['return_code'] != 'SUCCESS'){
            dump($order_data['return_msg']);die;
        }
        if($order_data['result_code'] != 'SUCCESS'){
            dump($order_data['err_code_des']);die;
        }
        return $order_data['mweb_url'];
    }


    function toRechargeCodePay($orderInfo,$config)
    {
        require_once VENDOR_PATH . "WeixinPay/lib/WxPay.NativePay.php";
        require_once VENDOR_PATH . "WeixinPay/lib/WxPay.Config.php";
        if(!empty($config)){
            \WxPayConfig::$APPID = $config['appid'];
            \WxPayConfig::$MCHID = $config['mchid'];
            \WxPayConfig::$KEY = $config['key'];
            \WxPayConfig::$APPSECRET = $config['appsecret'];
        }
        $notify = new \NativePay();
        $input  = new \WxPayUnifiedOrder();
        $input->SetBody($orderInfo['title']);
        $input->SetAttach("");
        $input->SetOut_trade_no($orderInfo['order_num']);
        $input->SetTotal_fee($orderInfo['discount_price'] * 100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 60*10));
        $input->SetGoods_tag("");
        $input->SetNotify_url($orderInfo['notify_url']);
        $input->SetTrade_type("NATIVE");
        $input->SetProduct_id('');
        try {
            $result = $notify->GetPayUrl($input);
            return $result["code_url"];
        } catch (\Exception $e) {
            var_dump($e->getMessage());
        }
    }

    function rechargeJsApiPay($orderInfo,$openid,$config)
    {
        require_once VENDOR_PATH . "WeixinPay/lib/WxPay.Api.php";
        require_once VENDOR_PATH . "WeixinPay/lib/WxPay.JsApiPay.php";
        require_once VENDOR_PATH . "WeixinPay/lib/WxPay.Config.php";
        if(!empty($config)){
            \WxPayConfig::$APPID = $config['appid'];
            \WxPayConfig::$MCHID = $config['mchid'];
            \WxPayConfig::$KEY = $config['key'];
            \WxPayConfig::$APPSECRET = $config['appsecret'];
        }
        $tools = new \JsApiPay();
        $input = new \WxPayUnifiedOrder();
        $input->SetBody($orderInfo['title']);
        $input->SetAttach('');
        $input->SetOut_trade_no($orderInfo['order_num']);
        $input->SetTotal_fee($orderInfo['discount_price'] * 100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 60*10));
        $input->SetGoods_tag("");
        $input->SetNotify_url($orderInfo['notify_url']);
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openid);
        $order = \WxPayApi::unifiedOrder($input, 6);
        if($order['result_code'] == 'FAIL'){
            echo $order['err_code_des'];exit();
        }
        $jsApiParameters = $tools->GetJsApiParameters($order);
        return $jsApiParameters;
    }

    public function OrderQuery($accountConfig,$pay_order_number){
        require_once \Env::get('vendor_path') . "WeixinPay/lib/WxPay.Api.php";
        $input = new \WxPayOrderQuery();
        $input->SetOut_trade_no($pay_order_number);
        $config = new \WxPayConfig();
        if(!empty($config)){
            \WxPayConfig::$APPID = $accountConfig['appid'];
            \WxPayConfig::$MCHID = $accountConfig['mchid'];
            \WxPayConfig::$KEY = $accountConfig['key'];
            \WxPayConfig::$APPSECRET = $accountConfig['appsecret'];
        }
        $res = \WxPayApi::orderQuery($input);
        return $res;
    }


    public function toVipPay($orderInfo,$config){
        require_once VENDOR_PATH . 'WeixinPay/lib/WxPay.Config.php';
        if(!empty($config)){
            \WxPayConfig::$APPID = $config['appid'];
            \WxPayConfig::$MCHID = $config['mchid'];
            \WxPayConfig::$KEY = $config['key'];
            \WxPayConfig::$APPSECRET = $config['appsecret'];
        }
        require_once VENDOR_PATH . 'WeixinPay/lib/WxPay.Api.php';
        $input = new \WxPayUnifiedOrder();
        $input->SetBody($orderInfo['title']);
        $input->SetAttach("");
        $input->SetOut_trade_no($orderInfo['order_num']);
        $input->SetTotal_fee($orderInfo['price'] * 100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 60*10));
        $input->SetNotify_url($orderInfo['notify_url']);
        $input->SetTrade_type("MWEB");
        $order_data = \WxPayApi::unifiedOrder($input,6);
        if($order_data['return_code'] != 'SUCCESS'){
            dump($order_data['return_msg']);die;
        }
        if($order_data['result_code'] != 'SUCCESS'){
            dump($order_data['err_code_des']);die;
        }
        return $order_data['mweb_url'];
    }


    function toVipCodePay($orderInfo,$config)
    {
        require_once VENDOR_PATH . "WeixinPay/lib/WxPay.NativePay.php";
        require_once VENDOR_PATH . "WeixinPay/lib/WxPay.Config.php";
        if(!empty($config)){
            \WxPayConfig::$APPID = $config['appid'];
            \WxPayConfig::$MCHID = $config['mchid'];
            \WxPayConfig::$KEY = $config['key'];
            \WxPayConfig::$APPSECRET = $config['appsecret'];
        }
        $notify = new \NativePay();
        $input  = new \WxPayUnifiedOrder();
        $input->SetBody($orderInfo['title']);
        $input->SetAttach("");
        $input->SetOut_trade_no($orderInfo['order_num']);
        $input->SetTotal_fee($orderInfo['price'] * 100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 60*10));
        $input->SetGoods_tag("");
        $input->SetNotify_url($orderInfo['notify_url']);
        $input->SetTrade_type("NATIVE");
        $input->SetProduct_id('');
        try {
            $result = $notify->GetPayUrl($input);
            return $result["code_url"];
        } catch (\Exception $e) {
            var_dump($e->getMessage());
        }
    }

    function vipJsApiPay($orderInfo,$openid,$config)
    {
        require_once VENDOR_PATH . "WeixinPay/lib/WxPay.Api.php";
        require_once VENDOR_PATH . "WeixinPay/lib/WxPay.JsApiPay.php";
        require_once VENDOR_PATH . "WeixinPay/lib/WxPay.Config.php";
        if(!empty($config)){
            \WxPayConfig::$APPID = $config['appid'];
            \WxPayConfig::$MCHID = $config['mchid'];
            \WxPayConfig::$KEY = $config['key'];
            \WxPayConfig::$APPSECRET = $config['appsecret'];
        }
        $tools = new \JsApiPay();
        $input = new \WxPayUnifiedOrder();
        $input->SetBody($orderInfo['title']);
        $input->SetAttach('');
        $input->SetOut_trade_no($orderInfo['order_num']);
        $input->SetTotal_fee($orderInfo['price'] * 100);
        $input->SetTime_start(date("YmdHis"));
        $input->SetTime_expire(date("YmdHis", time() + 60*10));
        $input->SetGoods_tag("");
        $input->SetNotify_url($orderInfo['notify_url']);
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openid);
        $order = \WxPayApi::unifiedOrder($input, 6);
        if($order['result_code'] == 'FAIL'){
            echo $order['err_code_des'];exit();
        }
        $jsApiParameters = $tools->GetJsApiParameters($order);
        return $jsApiParameters;
    }
}
